﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SVC.client;

namespace UI.UserControls
{
    public partial class ModelPower : DevExpress.XtraEditors.XtraUserControl
    {
        public BindingList<Power> Powers = null;
        public string 模块名称 { get; set; }
        public string 岗位编码 { get; set; }
        public int modelid { get; set; }
        public bool ReadOnly
        {
            
            set
            {

                foreach (CheckEdit ce in flowLayoutPanel1.Controls)
                {
                    ce.Properties.ReadOnly = value;
                }
            }
        }
        /// <summary>
        /// 有权限的
        /// </summary>
        public List<int> HasPowers { get; set; }

        public ModelPower()
        {
            InitializeComponent();


        }
        public void Init()
        {
            groupBox1.Text = 模块名称;

            foreach (var power in Powers)
            {
                CheckEdit ce = new CheckEdit();
                ce.Name = power.powername;
                ce.Text = power.powername; ;

                ce.Width += 39;
                ce.Tag = power.powerid;
                flowLayoutPanel1.Controls.Add(ce);
                ce.Show();
                
            }
            int h = this.Height;
            switch (模块名称)
            {
                case "基本信息":
                    h = 210;
                    break;
                case "库存管理":
                    h = 51;
                    break;
                case "采购管理":
                    h = 51;
                    break;
                case "销售管理":
                    h = 71;
                    break;
                case "业务管理": 
                    h = 71;
                    break;
                case "出纳管理":
                    h = 51;
                    break;
                default:
                    break;
            }
            
            this.Height = h;
        }
        public void Bind()
        {
            if (!string.IsNullOrWhiteSpace(岗位编码) && HasPowers != null)
            {
                foreach (CheckEdit ce in flowLayoutPanel1.Controls)
                {
                    ce.Checked = HasPowers.Count(p => p == (int)ce.Tag) > 0;
                    ce.CheckedChanged += new EventHandler(ce_CheckedChanged);
                }
            }

        }

        void ce_CheckedChanged(object sender, EventArgs e)
        {
            CheckEdit ce = (CheckEdit)sender;
            if (ce.Checked)
            {
                if (HasPowers.Count(p=>p == (int)ce.Tag) == 0)
                {
                    HasPowers.Add((int)ce.Tag);
                }
            }
            else
            {
                HasPowers.Remove((int)ce.Tag);
            }
            

        }


    }
}
